angular.module('itemDetail', ['ngAnimate', 'ui.bootstrap'])
    .controller('itemDetailController', ['$scope', '$http', function ($scope, $http) {
        $scope.myInterval = 5000;
        $scope.noWrapSlides = false;
        $scope.item = {};
        $scope.getItemDetail = function (itemId) {

            var data = {
                itemId: itemId,
                needSku: true
            };
            var transFn = function (data) {
                    return $.param(data);
                },
                postCfg = {
                    headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                    transformRequest: transFn
                };
            $http.post("/azeroth/api/item/getItemDetail.htm", data, postCfg).success(function (resultJson) {
                if (resultJson.success) {
                    $scope.item = resultJson.data;
                    //更改原价和优惠价
                    $scope.item.price = $scope.item.price / 100;
                    $scope.item.vipPrice = $scope.item.vipPrice / 100;
                    setTimeout(function () {
                        $("#owl-demo").owlCarousel({
                            navigation: false, // Show next and prev buttons
                            slideSpeed: 300,
                            paginationSpeed: 400,
                            singleItem: true,
                            autoPlay: 2000
                        });
                    }, 50);
                } else {
                    //console.log
                }
            }).error(function (e) {
                //console.log
            });
        }
        //获取页面参数
        $scope._getQueryString = function (name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                results = regex.exec(location.href);
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
        $scope.getItemDetail($scope._getQueryString("itemId"));
        $scope.goApp = function () {
            var ifr = document.createElement('iframe');
            ifr.src = 'tel://13750824836';
            ifr.style.display = 'none';
            document.body.appendChild(ifr);
            //记录唤醒时间
            var openTime = +new Date();
            window.setTimeout(function () {
                document.body.removeChild(ifr);
                //如果setTimeout 回调超过2500ms，则弹出下载
                if ((+new Date()) - openTime > 2500) {
                    window.location = 'http://www.baidu.com';
                }
            }, 2000)
        }
    }]);